<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

/**
 * 课程章节
 */
class Chapter extends Backend
{
    /**
     * 当前控制器下的一个模型属性
     */

    protected $relationSearch = true;
    protected $searchFields = 'id,title,createtime';
    protected $ChapterModel = null;

    // 初始化
    public function _initialize()
    {
        parent::_initialize();
        // 引入的模型
        $this->ChapterModel = model('Subject.Chapter');
    }
    public function index($courseid = null)
    {
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->ChapterModel
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count(); //查询总数


            $list = $this->ChapterModel
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select(); //查询数据


            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }
    public function add()
    {
        $subid = $this->request->get('courseid');
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            $params['subid'] = $subid;
            if ($params) {
                $result = $this->ChapterModel->validate('common/subject/chapter')->save($params);
                if ($result === FALSE) {
                    $this->error($this->ChapterModel->getError());
                    exit;
                }
                $this->success();
                exit;
            }
        }
        return $this->view->fetch();
    }
    public function edit($ids = null)
    {
        $row = $this->ChapterModel->find($ids);

        if (!$row) {
            $this->error(__('No Results were found'));
            exit;
        }

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            if ($params) {
                $params['id'] = $ids;
                $params['subid'] = $row['subid'];
                $result = $this->ChapterModel->validate("Common/Subject/Chapter")->isUpdate(true)->save($params);

                if ($result === false) {

                    if($params['url'] && $params['url'] !=  $row['url']) {
                        $thumbs = substr($params['url'], 1); 
                        @is_file($thumbs) && @unlink($thumbs);
                    }

                    $this->error($this->ChapterModel->getError());
                    exit;
                } else {
                    if($params['url'] && $params['url'] !=  $row['url']) {
                        $thumbs = substr($row['url'], 1);
                        @is_file($thumbs) && @unlink($thumbs);
                    }
                }

                $this->success();
                exit;
            }
        }
        $this->assign("row", $row);
        return $this->view->fetch();
    }
    public function del($ids = null)
    {
        $ids = !empty($ids) ? explode(",", $ids) : [];
        $rows = $this->ChapterModel->column('id');
        foreach ($ids as $item) {
            if (!in_array($item, $rows)) {
                $this->error(__('没有找到给课程章节'));
                exit;
            }
        }
        // 删除视频
        $where = ['id' => ['in', $ids]];
        $row=$this->ChapterModel->where($where)->column('url');
        if(count($ids)>=2)
        {
            foreach($row as $item)
            {
                $path=pathinfo($item,PATHINFO_DIRNAME);
                // $thumbs=substr($item,1);
                var_dump($item);
                // $this->success('',null,$thumbs);
                @is_file('.'.$item) && @unlink('.'.$item);
                @rmdir('.'.$path);
            }
            // exit;
        }else
        {
            if($row)
            {
                $row=implode('',$row);
                $path=pathinfo($row,PATHINFO_DIRNAME);
                // $thumbs=substr($row,1);
                // var_dump($row);
                // $this->success('找到了',null,$row);
                $res=@is_file('.'.$row) && @unlink('.'.$row);
                @rmdir('.'.$path);
            }
        }
        // 执行删除操作
        $result = $this->ChapterModel->where(['id'=>['in',$ids]])->delete();
        if ($result === FALSE) {
            $this->error($this->ChapterModel->getError());
            exit;
        }
        // exit;

        $this->success();
        exit;
    }
}
